package com.hellogwt.client.presenter;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;
import com.hellogwt.client.event.EditGreetingCancelledEvent;
import com.hellogwt.client.service.GreetingServiceAsync;
import com.hellogwt.shared.domain.Greeting;

public class EditGreetingPresenter implements Presenter {

    public interface Display {
        HasValue<String> getAuthorTextBox();

        HasValue<String> getTextTextBox();

        HasClickHandlers getAddButton();

        HasClickHandlers getUpdateButton();

        HasClickHandlers getDeleteButton();

        HasClickHandlers getCancelButton();

        Widget asWidget();
    }

    private GreetingServiceAsync greetingService = null;
    private HandlerManager eventBus = null;
    private Display display = null;

    public EditGreetingPresenter(GreetingServiceAsync greetingService, HandlerManager eventBus, Display display) {
        this.greetingService = greetingService;
        this.eventBus = eventBus;
        this.display = display;

        bind();
    }

    @Override
    public void go(HasWidgets container) {
        container.clear();
        container.add(display.asWidget());
    }

    private void bind() {
        final AsyncCallback<Void> callback = new AsyncCallback<Void>() {
            @Override
            public void onFailure(Throwable caught) {
                Window.alert("ERROR: Cannot edit greetings!");
            }

            @Override
            public void onSuccess(Void result) {
                eventBus.fireEvent(new EditGreetingCancelledEvent());
            }
        };

        display.getAddButton().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {
                if (!display.getAuthorTextBox().getValue().isEmpty()
                        && !display.getTextTextBox().getValue().isEmpty()) {
                    greetingService.getGreeting(display.getTextTextBox().getValue(), new AsyncCallback<Greeting>() {
                        @Override
                        public void onFailure(Throwable caught) {
                            Window.alert("ERROR: Cannot find greeting!");
                        }

                        @Override
                        public void onSuccess(Greeting result) {
                            if (result == null) {
                                greetingService.addGreeting(display.getAuthorTextBox().getValue(),
                                        display.getTextTextBox().getValue(), callback);
                            } else {
                                Window.alert("Greeting already exists!");
                            }
                        }
                    });
                } else {
                    Window.alert("\"Author\" and \"Text\" fields cannot be empty!");
                }
            }
        });

        display.getUpdateButton().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                if (!display.getAuthorTextBox().getValue().isEmpty()
                        && !display.getTextTextBox().getValue().isEmpty()) {
                    greetingService.updateGreeting(display.getAuthorTextBox().getValue(),
                            display.getTextTextBox().getValue(), callback);
                } else {
                    Window.alert("\"Author\" and \"Text\" fields cannot be empty!");
                }
            }
        });

        display.getDeleteButton().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                greetingService.deleteGreeting(display.getTextTextBox().getValue(), callback);
            }
        });

        display.getCancelButton().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                eventBus.fireEvent(new EditGreetingCancelledEvent());
            }
        });
    }
}